package com.linux.huhx.business.action;

import com.linux.huhx.base.BaseController;
import com.linux.huhx.business.service.LoginService;
import com.linux.huhx.constants.Constants;
import com.linux.huhx.constants.ConstantsMsg;
import com.linux.huhx.utils.DateFormatUtils;
import com.linux.huhx.utils.EncryptUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by huhx on 2017-05-15.
 */
public class LoginAction extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(LoginAction.class);
    private LoginService loginService;

    public void setLoginService(LoginService loginService) {
        this.loginService = loginService;
    }

    @Override
    protected Map handleInner(HttpServletRequest request, HttpServletResponse response) throws Exception {
//        String contextPath = request.getContextPath();
//        String servletPath = request.getServletPath();
//        String realPath = request.getServletContext().getRealPath( "/");
//
//        System.out.println(contextPath);
//        System.out.println(servletPath);
//        System.out.println(realPath);

        String username = (String) request.getAttribute("username");
        String password = (String) request.getAttribute("password");

        System.out.println(username + ", " + password);

        Map map = new HashMap();
        map.put("username", "huhx");
        map.put("password", "word");
        map.put("checked", "true");
        map.put("message", "I love a gril named liuling.");
        return map;
//        Map<String, String> map = loginService.sayHello();
//        Map<String, String> resultmap = new HashMap<>(map);
//        resultmap.put("return nullCode", "0000");
//        resultmap.put("return nullMsg", "I lvoe you");
//        return null resultmap;
        // 得到数据
//        String email = (String) request.getAttribute("Email");
//        String password = (String) request.getAttribute("Password");
//        String usertype = (String) request.getAttribute("UserType");
//        String nowDate = DateFormatUtils.formatDate(new Date());
//
//        // 用户的邮箱不能为空
//        if (StringUtils.isEmpty(email)) {
//            return null;
//        }
//        // 用户的类型不能为空
//        if (StringUtils.isEmpty(usertype)) {
//            return null;
//        }
//        // 用户的密码不能为空
//        if (StringUtils.isEmpty(password)) {
//            return null;
//        }
//
//        // 对密码加密
//        String encryptPassword = EncryptUtils.md5Encrypt(password);
//        logger.info("email: " + email + ", password: " + encryptPassword);
//
//        // 校验邮箱是否存在
//        Long userId = loginService.queryUserIdByEmail(email);
//        if (StringUtils.isEmpty(userId)) {
//            return null;
//        }
//        // 校验今天失败的次数，如果大于5。这个设置为常量
//        Map<String, Object> failParamMap = new HashMap<>();
//        failParamMap.put("userId", userId);
//        failParamMap.put("dayTime", nowDate);
//        int failLoginCount = loginService.queryFailLoginCount(failParamMap);
//        if (failLoginCount >= Constants.MAX_FAIL_LOGIN_COUNT) {
//            return null;
//        }
//
//        // 校验密码是否正确
//        Map<String, String> paramMap = new HashMap<>();
//        paramMap.put("email", email);
//        paramMap.put("userStatus", Constants.USER_STATUS_NORMAL);
//        paramMap.put("password", encryptPassword);
//        paramMap.put("usertype", usertype);
//        if (!loginService.checkPasswordMatch(paramMap)) {
//            // 更新登录失败次数，由于注册的插入登录信息，这里对应会有一个信息。
//            Map<String, Object> failCountMap = new HashMap<>();
//            failCountMap.put("userId", userId);
//            failCountMap.put("userStatus", Constants.USER_STATUS_NORMAL);
//            failCountMap.put("dayTime", nowDate);
//            failCountMap.put("loginIp", request.getRemoteAddr());
//            loginService.updateFailLoginCount(failCountMap);
//            return null;
//        }
//
//        // 成功，更新登录时间
//        Map<String, Object> successParamMap = new HashMap<>();
//        successParamMap.put("userId", userId);
//        successParamMap.put("loginIp", request.getRemoteAddr());
//        successParamMap.put("dayTime", nowDate);
//        loginService.updateUserLoginTime(successParamMap);
//
//        // 返回数据
//        Map<String, Object> map = new HashMap<>();
//        map.put("email", email);
//        map.put("userId", userId);
//        return null;
    }
}
